/*--------------------------------------------------------------
-- Use the next lines if you want to drop/create database

use [master]
GO

drop database [FlowDocs]
GO

create database [FlowDocs]
GO

use [FlowDocs]
GO
*/

IF (EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'Attachment'))
BEGIN
	RAISERROR ('The database is not empty, please drop the database before running this script', 15, 1)
END


/*--------------------------------------------------------------

CREATE DATABASE

--------------------------------------------------------------*/


CREATE TABLE [dbo].[Attachment] (
    [AttachmentId] [int] NOT NULL IDENTITY,
    [FileName] [nvarchar](100),
    [DataField] [varbinary](max),
    [Partial] [int] NOT NULL,
    [DocumentId] [int],
    [OidDocument] [uniqueidentifier],
    [DateCreated] [datetime],
    CONSTRAINT [PK_dbo.Attachment] PRIMARY KEY ([AttachmentId])
)
CREATE TABLE [dbo].[Document] (
    [DocumentId] [int] NOT NULL IDENTITY,
    [OidDocument] [uniqueidentifier] NOT NULL,
    [DocumentName] [nvarchar](100),
    [Description] [nvarchar](200),
    [Owner] [nvarchar](16),
    [Path] [nvarchar](256),
    [Version] [int] NOT NULL,
    [MimeType] [nvarchar](200),
    [FileHash] [nvarchar](20),
    [FileSize] [bigint] NOT NULL,
    [DocumentPreviousId] [int],
    [DateCreated] [datetime] NOT NULL,
    [DateLastUpdated] [datetime] NOT NULL,
    CONSTRAINT [PK_dbo.Document] PRIMARY KEY ([DocumentId])
)
CREATE INDEX [IX_DocumentPreviousId] ON [dbo].[Document]([DocumentPreviousId])
CREATE INDEX [IX_DocumentId] ON [dbo].[Attachment]([DocumentId])
ALTER TABLE [dbo].[Attachment] ADD CONSTRAINT [FK_dbo.Attachment_dbo.Document_DocumentId] FOREIGN KEY ([DocumentId]) REFERENCES [dbo].[Document] ([DocumentId]) ON DELETE CASCADE
ALTER TABLE [dbo].[Document] ADD CONSTRAINT [FK_dbo.Document_dbo.Document_DocumentPreviousId] FOREIGN KEY ([DocumentPreviousId]) REFERENCES [dbo].[Document] ([DocumentId])
CREATE TABLE [dbo].[__MigrationHistory] (
    [MigrationId] [nvarchar](150) NOT NULL,
    [ContextKey] [nvarchar](300) NOT NULL,
    [Model] [varbinary](max) NOT NULL,
    [ProductVersion] [nvarchar](32) NOT NULL,
    CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY ([MigrationId], [ContextKey])
)
INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'201311180344219_AutomaticMigration', N'Flow.Docs.Data.Migrations.Configuration',  0x1F8B0800000000000400DD5ACD72DB3610BE77A6EFC0E1A9ED4C44DB693269464AC691ADC4D3F8672C27D70C44AE244C41902140C7EEABF5D047EA2B74418A7F00499194ECC6BDD9E0E2C3EE627701ECA77FFEFA7BFCF6CE67D62D4482067C621F8E0E6C0BB81B7894AF26762C97CF5ED96FDFFCF8C3F8D4F3EFACCF99DC73258733B998D86B29C3D78E23DC35F8448C7CEA4681089672E406BE43BCC0393A38F8CD393C7400216CC4B2ACF175CC25F521F907FF9D06DC8550C6849D071E30B119C72FF304D5BA203E8890B830B1672CF8363A095C313A2192D8D631A304B598035BDA16E13C9044A28EAF3F0998CB28E0AB79880384DDDC8780724BC2046C747F5D887735E3E04899E1141307B9C1CE0D44134FD115F25EA9979839B18FA524EEDA072ECB7228F93BDC570670E82A0A4288E4FD352C8DD9679E6D3955044787C8016A672BA526F61997CF8F6CEB22668C2C18E43E2C397B2E8308DE03878848F0AE889410A14FCF3C488C33F4D0569D5106EAAF6C45DC388C3FDB3A27771F81AFE41A23F300236E46EFC0CB46365A7CE214C31527C928866D0BA990995160B96DEF2827D17D6525FCB37EA576E82B12A930DBE6B42DFA056E5CEBFCF66997D4CB6666F3DEC7D4DC7ED31B308D406D59364D0DDD50DF54F482DCD255B2D90D2ADBD635B04440AC699826E4A808A72F85DC2C0AFCEB8055A22DFFFC651EC491AB22216896B921D10A6455C7B15324526B7AE58A0C48AEF2FEF44DADC6BD7DC0C46A0C8C8171F938490AC28D689856D7C6B58EF6B2D6E537746D9B452FF7B0086EDBBACD9217FB58647336EF567FCE31F5D5FC07F7BBAAF81F886875CBBED699D33FA1E49697BF0E0EFFAB086E69108B9EE5B9B5CEF65505277E24427E0ABD61785B0B7966646D41CF4BB0295D94F54621A3B8374BF629F1C742042E4D54D56C31D5AC3AE3947B56679DD3402D7B01E3356692868CBAA8D6C4FEC5F076970532533B2C70301A1D1A6BE0F104913A0F08C38BB49011A15C9A6719E52E0D09EBAA8E06D0F338549B952FA97F398110B83AC1BAFABE8F2EE51C3575CA97D60EEF6D3E1C3BA5206B8FBDBA3B4F5350B45E808A7028BD087A445CDBCDA96BACE90EBCE427C0408275ECA6CFA629112EF1CC3283A9EAED214C5B6C78C4006DD9A6DDB5D87B48A64512E7489C01517612E2BB593D9B93AF14CC4AA826CD411A518CA5BD28BB350169C45F152833BE0EA6083D0DA46499895473F694E4B71E54FA06F4390372E32A76197BDAA7EA7781AC6C358A767056EDCBCB74D3D662D5B95C95ECA8C44E8B73DA0AD48E6EC9AE0A7916142D2527ED2965BD27A7A1F9343E27618877D252336A3362CDD34ED4F4D9BC7FF7C74F311C57D43481726DF395F0F94756A07DC5A551D3198D84540D8D0551D7BCA9E71B625D733E5BCE4C7D73F3B214CEE6A8BF8B0A5374E6464D6DACB22B67689D92480C85DA103227276D41C248D4DABB9A062CF6F9F69E581B62D1972AA315A3DD914A8DA7325469B83B56DE692A23E5833D742A1D4B15A55A8EAB66B44AABA10C57F9D0CB63C5A349F359F1C1C41B3B5A6C1907AE11C3C6CDA59A169D92A628523BA44C5309EE9030CD539FE8F6573A4E75FAF54EC1725BA90258FED0C3E2B47554B1351DEA93CAAA3354CD6335D21D216FFB9441F2C1EE3845E7A70C548CF62B9A696B472F9AE9683FA4B479A323A5A3FDA3A9FC38AC8BA9B6C7630BFAC052D58657E9F1E898958F8F5C028D9B952E92AF9EDFB0B49BD47873ABD9CEF519D79C54C4B6D061B7D453579CF9BD90E0A7E573FE954D194D0A4D26704E385D829037C11FC093E6E52B8D321C40DF394278EC6971785439652B99D0B317A9D376FC9644EE9A44262750000F62E910779110753FF9E4EEE7BE701A3397B862675E4E43E9C43598644CCCE9D71868B2014BAACE8EDE9ED23BCBAA2A48AD136C423D41B6EC4142B8E79E0C26CBF6961A2637560B7DD41FBA4285D5EBFBB27FEA15CC57BD9E2F7A636A44D79074D669AE7DB95067B51A7087C09649AC055DED52C44C166B4031EB587976E1B43AE23D062B349C9C31B1D26BC720366927A6E7BF6074125BFFB7EC4DFFE8D8239D52D7461D4C1A3D99C06A6D2A7EA7CC8BD9ACED4ABD6C615ED2D71016CA45803B9F16C801CC4C2B3153B7C6774ADBD492069DD899B4247798FDBD1031C34D36834BEFB73F08C9623EF931CD4A3F02C64417745540A89F0473702B0996CB9CF16590E5BAA65126A25FF84012BC4E90637C092E898B5115B82044F293A3CF84C52872EA2FC03BE397B10C63892683BF6095DFD7A97AD1B67EC22455751E5F263775B10F13504DAA6E4497FC5D4CD5D378A3F7ACE646D400A10AD1E6EDA4F652AA37D4EA3E47BA087847A08DFBF2FA79037EC8104C5CF239B98566DDB6FBB0EAB1F10925AB88F8628351CCC77F31FC3CFFEECDBFB1FDFDACC62E0000 , N'6.0.1-21010')

GO
